package cloudoer.su.action;

import cloudoer.su.base.impl.BaseAction;
import cloudoer.su.entity.Menu;
import cloudoer.su.entity.Role;
import cloudoer.su.entity.User;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@Scope("prototype")
public class RoleAction extends BaseAction {
    private Role role;

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String indexUI (){
        return "success";
    }

    public String addUI(){
        return "success";
    }

    public String importUI(){
        return "success";
    }

    public String menuUI(){
        return "success";
    }

    public String userUI(){
        return "success";
    }

    public String getAll() throws IOException {
        String q = ServletActionContext.getRequest().getParameter("q");
        List<Role> list = roleService.getAll(q);
        ajaxJson(list,new String[]{"menus","users"});
        return null;
    }

    public String getByPage() throws IOException {
        int pageNo = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
        int pageSize = Integer.parseInt(ServletActionContext.getRequest().getParameter("rows"));
        List<Role> list = roleService.getByPage(pageNo, pageSize);
        ajaxDataGrid(roleService.getCount(), list, new String[]{"menus","users"});
        return null;
    }

    public String getById () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        Role role = roleService.getById(id);
        ajaxJson(role, new String[]{"menus","users"});
        return null;
    }

    public String getByNumber () throws IOException {
        String number = ServletActionContext.getRequest().getParameter("number");
        Role role = roleService.getById(number);
        ajaxJson(role, new String[]{"menus","users"});
        return null;
    }

    public String add() throws IOException {
        roleService.add(role);
        ajaxSuccess("添加成功");
        return null;
    }

    public String update() throws IOException {
        roleService.update(role);
        ajaxSuccess("修改成功");
        return null;
    }

    public String delete() throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        roleService.delete(id);
        ajaxSuccess("删除成功");
        return null;
    }

    public String importFile() throws IOException {
        Map<String, Object> map = getExcalFile();
        File file = (File) map.get("file");
        String fileName = (String) map.get("name");
        if (file == null){
            ajaxError("文件为空，导入失败");
            return null;
        }
        if (fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){//检查是否是Excel文件
            String msg = roleService.importFile(file);
            ajaxSuccess(msg);
        }else {
            ajaxError("文件格式错误，请按规范提交");
            return null;
        }
        return null;
    }

    public String exportFile() throws Exception {
        OutputStream os = ServletActionContext.getResponse().getOutputStream();
        ServletActionContext.getResponse().setContentType("application/x-execl");
        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String("角色数据.xls".getBytes(), "ISO-8859-1"));
        roleService.exportFile(os);
        return null;
    }

    public String getUsers () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        Set<User> set = roleService.getUsers(id);
        ajaxJson(set,new String[]{"roles","classes","dormitory","classCommittees"});
        return null;
    }

    public String setUsers () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        String userIds = ServletActionContext.getRequest().getParameter("userIds");
        roleService.setUsers(id, userIds.split("▁"));
        ajaxSuccess("设置成功");
        return null;
    }

    public String getMenus () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        Set<Menu> set = roleService.getMenus(id);
        ajaxJson(set, new String[]{"superior","lower"});
        return null;
    }

    public String setMenus () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        String menuIds = ServletActionContext.getRequest().getParameter("menuIds");
        String[] ids =  menuIds.split("▁");
        roleService.setMenus(id,ids);
        ajaxSuccess("设置成功");
        return null;
    }

    public String deleteMenus() throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        String menuId = ServletActionContext.getRequest().getParameter("menuId");
        roleService.deleteMenus(id, menuId);
        ajaxSuccess("删除成功");
        return null;
    }

    public String deleteUsers() throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        String userId = ServletActionContext.getRequest().getParameter("userId");
        roleService.deleteUsers(id, userId);
        ajaxSuccess("删除成功");
        return null;
    }
}
